package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.i.accounting.api.parameter.AccountEntry;
import com.ymatou.iapi.trading.operate.parameter.req.OrderPayCallBackReq;
import com.ymatou.iapi.trading.operate.service.OrderPostPayCallBackCall;
import com.ymatou.operate.trading.api.parameter.PlaceOrderBean;
import com.ymatou.operate.trading.api.service.PlaceOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.business.service.ProductCallService;
import com.ymttest.common.entitie.OrderInfo;
import com.ymttest.common.entitie.OrderProductInfo;
import com.ymttest.common.entitie.ProductActivityInfo;
import com.ymttest.common.enums.ActivityPromotionTypeEnum;
import com.ymttest.common.enums.AppTerminalSourceEnum;
import com.ymttest.common.enums.BondedAreaEnum;
import com.ymttest.common.enums.LogisticsTypeEnum;
import com.ymttest.common.enums.OrderSourceEnum;
import com.ymttest.common.enums.OrderTypeEnum;
import com.ymttest.common.enums.PriceTypeEnum;
import com.ymttest.common.enums.ProductRefundChannelEnum;
import com.ymttest.common.enums.StockStatusEnum;
import com.ymttest.common.enums.TerminalSourceEnum;
import com.ymttest.database.model.YmtAccountEntry;
import com.ymttest.database.sqlwapper.AccountDefaultWapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 第三方支付尾款回调通知接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_OrderPostPayCallBack {
    private static OrderPayCallBackReq orderPayCallBackBean;
    private static OrderPostPayCallBackCall orderPostPayCallBackCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("第三方支付尾款回调通知接口");
    }

    @Before
    public void caseUp() {

        orderPayCallBackBean = new OrderPayCallBackReq();
        orderPostPayCallBackCall = new OrderPostPayCallBackCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(orderPostPayCallBackCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_OrderPostPayCallBack_001() {
        Logger.start(true, "第三方补款,成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderCallService = new OrderCallService();
            Map map = new OrderCallService().appSaveOrder();
            int orderId = (Integer) map.get("orderId");
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            BigDecimal needPayAmout = orderCallService.getNeedPrePayAmountOfCash(orderId);
            // 支付
            // 13 为 app国际支付宝，15 微信
            String payType = "13";
            orderCallService.orderPayRequestInJava(userId, needPayAmout, tradingId, payType);
            // 接单
            orderCallService.acceptOrder(orderId, sellerId);
            // 卖家发起补款
            BigDecimal postPayAmout = new BigDecimal(1 + new Random().nextInt(50));
            String postPayId = new OrderCallService().applyForPostPayInJava(orderId, sellerId, postPayAmout);
            BigDecimal blancePostPayAmout = BigDecimal.ZERO;
            BigDecimal thirdPostPayAmout = postPayAmout.subtract(blancePostPayAmout);
            // 补款
            orderCallService.orderPostPayRequestInJava(userId, blancePostPayAmout, orderId, payType);
            Logger.comment("前置数据准备结束");
            // 第三方补款交易号
            List<Map> thirdPostPayTradingInfo = new DefaultWapper().selectPostPayTradingInfo(orderId);
            String thirdPostPayTradingId = (String) thirdPostPayTradingInfo.get(0).get("PostPayTradingId");
            // 查询支付指令
            HashMap paymentInstruction = new HashMap();
            paymentInstruction =
                    new DefaultWapper().selectPaymentInstructionByNewtradingId(thirdPostPayTradingId);
            String trackId = (String) paymentInstruction.get("id");
            orderPayCallBackBean.setAppId("1");
            orderPayCallBackBean.setBizCode("3");
            orderPayCallBackBean.setCurrency("CNY");
            orderPayCallBackBean.setMemo("autotest");
            orderPayCallBackBean.setTradingId(String.valueOf(thirdPostPayTradingId));
            orderPayCallBackBean.setPaymentId("radamnumber");
            // 此接口的价格不用传，因为接口不会按照这个参数进行支付。
            orderPayCallBackBean.setPayPrice("price");
            orderPayCallBackBean.setPayTime("2015-09-09T22:53:22");
            orderPayCallBackBean.setSignMethod("");
            orderPayCallBackBean.setTraceId(trackId);
            orderPayCallBackBean.setInstPaymentId("radamInstPaymentId");
            orderPayCallBackBean.setInternalUserId("internaluseid");
            orderPayCallBackBean.setExternalUserId("radamuserId");
            orderPayCallBackBean.setPayChannel("Alipay");
            orderPayCallBackBean.setPayType("type");
            orderPayCallBackBean.setVersion(1);
            // 发送请求
            orderPostPayCallBackCall.setData(orderPayCallBackBean);
            orderPostPayCallBackCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, orderPostPayCallBackCall.getCode(), "验证Code");
            // 数据库验证
            // 支付后金额
            Thread.sleep(3000);
            Map act = new HashMap();
            act.put("bizno", String.valueOf(orderId));
            act.put("userId", userId);         
    		List<YmtAccountEntry> actinfos = new AccountDefaultWapper().selectAccountEntryByBizNo(act);
    		//计算支付+补尾款 资金流水金额
    		BigDecimal accountEntryPrice=actinfos.stream().map(a->a.getAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
    		
            HashMap<String, Integer> m = new HashMap<String, Integer>();
            m.put("OrderId", orderId);
            m.put("TradingStatus", 17);
            m.put("TraddingId", orderId);
            m.put("PaidInFull", 1);
            m.put("UserId", userId);
            Logger.verifyEquals(1, new DefaultWapper().selectOrder(m).size(), "验证订单表记录");
            Logger.verifyEquals(2, new DefaultWapper().selectTradingStatus(m).size(), "验证交易表记录");
            Logger.verifyEquals(needPayAmout.add(postPayAmout), accountEntryPrice.divide(new BigDecimal(-1),2,4), "验证账户资金流水金额");
            Logger.verifyEquals(1, new DefaultWapper().selectOrderPostPayByOrderId(orderId).get(0).get("iAction"),
                    "验证补款Action状态");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_OrderPostPayCallBack_002() {
        Logger.start(true, "一笔交易里有多个订单,余额+第三方支付宝混合支付,第三方支付通知成功");
        try {
            DefaultWapper database = new DefaultWapper();
            OrderCallService orderCallService = new OrderCallService();
            // 创建订单
            Logger.comment("前置数据准备");
            Map map = placeOrder();
            Logger.comment("前置数据准备结束");
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            JSONArray orders = (JSONArray) map.get("orders");
            int tradingId = (Integer) map.get("tradingId");
            // 获取用户需要支付的金额
            BigDecimal needPayAmount = orderCallService.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
            // 13 为 app国际支付宝，15 微信
            String payType = "15";
            orderCallService.orderPayRequestInJava(userId, needPayAmount, tradingId, payType);
            int orderId1 = orders.getInt(0);
            int orderId2 = orders.getInt(1);
            // 卖家接单
            orderCallService.acceptOrder(orderId1, sellerId);
            // 卖家接单
            orderCallService.acceptOrder(orderId2, sellerId);
            // 订单1 需要补款金额
            BigDecimal order1NeedPostPayAmout =
                    orderCallService.getPayableAmount(orderId1).subtract(
                            orderCallService.getNeedPrePayAmountOfCash(orderId1));
            // 订单2 需要补款金额
            BigDecimal order2NeedPostPayAmout =
                    orderCallService.getPayableAmount(orderId1).subtract(
                            orderCallService.getNeedPrePayAmountOfCash(orderId2));
            // 申请补款1
            orderCallService.applyForPostPay(orderId1, sellerId, order1NeedPostPayAmout);
            // 申请补款2
            orderCallService.applyForPostPay(orderId2, sellerId, order2NeedPostPayAmout);
            // 余额
            BigDecimal postPayBlance = new BigDecimal(9);
            // 补款
            orderCallService.orderPostPayRequest(userId, postPayBlance, orderId1,
                    payType);
            // 第三方补款交易号
            List<Map> thirdPostPayTradingInfo = new DefaultWapper().selectPostPayTradingInfo(orderId1);
            String thirdPostPayTradingId = (String) thirdPostPayTradingInfo.get(0).get("PostPayTradingId");
            // 查询支付指令
            HashMap paymentInstruction = new HashMap();
            paymentInstruction =
                    new DefaultWapper().selectPaymentInstructionByNewtradingId(thirdPostPayTradingId);
            String trackId = (String) paymentInstruction.get("id");
            Logger.comment("前置数据准备结束");
            orderPayCallBackBean.setAppId("1");
            orderPayCallBackBean.setBizCode("3");
            orderPayCallBackBean.setCurrency("CNY");
            orderPayCallBackBean.setMemo("autotest");
            orderPayCallBackBean.setTradingId(String.valueOf(thirdPostPayTradingId));
            orderPayCallBackBean.setPaymentId("radamnumber");
            // 此接口的价格不用传，因为接口不会按照这个参数进行支付。
            orderPayCallBackBean.setPayPrice("price");
            orderPayCallBackBean.setPayTime("2015-09-09T22:53:22");
            orderPayCallBackBean.setSignMethod("");
            orderPayCallBackBean.setTraceId(trackId);
            orderPayCallBackBean.setInstPaymentId("radamInstPaymentId");
            orderPayCallBackBean.setInternalUserId("internaluseid");
            orderPayCallBackBean.setExternalUserId("radamuserId");
            orderPayCallBackBean.setPayChannel("Alipay");
            orderPayCallBackBean.setPayType("type");
            orderPayCallBackBean.setVersion(1);
            // 发送请求
            orderPostPayCallBackCall.setData(orderPayCallBackBean);
            orderPostPayCallBackCall.callService();

            // 支付后金额
            Map act = new HashMap();
            act.put("bizno", String.valueOf(orderId1));
            act.put("userId", userId);   
            act.put("originalNo", String.valueOf(orderId1));
    		List<YmtAccountEntry> actinfos = new AccountDefaultWapper().selectAccountEntryByBizNo(act);
            
            Logger.verifyEquals(order1NeedPostPayAmout, actinfos.get(0).getAmount().divide(new BigDecimal(-1),2,4), "验证账户资金流水订单补款金额");
            
            int orderId = orderId1;
            // Ymt_PostPayTradingInfo 表
            List<Map> ymtPostPayTading = database.selectPostPayTradingInfo(orderId);
            // 订单表
            List<Map> ymtOrders = database.selectOrderByOderId(orderId);
            // 订单资金表
            List<Map> ymtOrderStateList = database.selectOrderStateByOderId(orderId);
            // 当前子订单需要支付的金额
            needPayAmount = new OrderCallService().getNeedPrePayAmountOfCash(orderId);
            Logger.comment("验证Ymt_Orders表");
            Logger.verifyEquals(
                    17,
                    (Integer) ymtOrders.get(0).get("iTradingStatus"), "验证iTradingStatus");
            Logger.comment("验证ymt_orderstate表");
            Logger.verifyEquals(
                    order1NeedPostPayAmout,
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfCash"), "验证fPostPaidAmountOfCash");
            Logger.verifyEquals(
                    order1NeedPostPayAmout.subtract(postPayBlance),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfThirdParty"),
                    "验证fPostPaidAmountOfThirdParty");
            Logger.verifyEquals(
                    postPayBlance.setScale(2),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfYmtAccount"),
                    "验证fPostPaidAmountOfYmtAccount");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_OrderPostPayCallBack_003() {
        Logger.start(true, "一笔交易里有多个订单,余额+第三方微信混合支付,第三方支付通知成功");
        try {
            DefaultWapper database = new DefaultWapper();
            OrderCallService orderCallService = new OrderCallService();
            // 创建订单
            Logger.comment("前置数据准备");
            Map map = placeOrder();
            Logger.comment("前置数据准备结束");
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            JSONArray orders = (JSONArray) map.get("orders");
            int tradingId = (Integer) map.get("tradingId");
            // 获取用户需要支付的金额
            BigDecimal needPayAmount = orderCallService.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
            // 全款第三方支付
            // 13 为 app国际支付宝，15 微信
            String payType = "15";
            orderCallService.orderPayRequestInJava(userId, needPayAmount, tradingId, payType);
            int orderId1 = orders.getInt(0);
            int orderId2 = orders.getInt(1);
            // 卖家接单
            orderCallService.acceptOrder(orderId1, sellerId);
            // 卖家接单
            orderCallService.acceptOrder(orderId2, sellerId);
            // 订单1 需要补款金额
            BigDecimal order1NeedPostPayAmout =
                    orderCallService.getPayableAmount(orderId1).subtract(
                            orderCallService.getNeedPrePayAmountOfCash(orderId1));
            // 订单2 需要补款金额
            BigDecimal order2NeedPostPayAmout =
                    orderCallService.getPayableAmount(orderId1).subtract(
                            orderCallService.getNeedPrePayAmountOfCash(orderId2));
            // 申请补款1
            orderCallService.applyForPostPay(orderId1, sellerId, order1NeedPostPayAmout);
            // 申请补款2
            orderCallService.applyForPostPay(orderId2, sellerId, order2NeedPostPayAmout);
            // 余额
            BigDecimal postPayBlance = new BigDecimal(9);
            orderCallService.orderPostPayRequest(userId, postPayBlance, orderId1,
                    payType);
            // 第三方补款交易号
            List<Map> thirdPostPayTradingInfo = new DefaultWapper().selectPostPayTradingInfo(orderId1);
            String thirdPostPayTradingId = (String) thirdPostPayTradingInfo.get(0).get("PostPayTradingId");
            // 查询支付指令
            HashMap paymentInstruction = new HashMap();
            paymentInstruction =
                    new DefaultWapper().selectPaymentInstructionByNewtradingId(thirdPostPayTradingId);
            String trackId = (String) paymentInstruction.get("id");
            Logger.comment("前置数据准备结束");
            orderPayCallBackBean.setAppId("1");
            orderPayCallBackBean.setBizCode("3");
            orderPayCallBackBean.setCurrency("CNY");
            orderPayCallBackBean.setMemo("autotest");
            orderPayCallBackBean.setTradingId(String.valueOf(thirdPostPayTradingId));
            orderPayCallBackBean.setPaymentId("radamnumber");
            // 此接口的价格不用传，因为接口不会按照这个参数进行支付。
            orderPayCallBackBean.setPayPrice("price");
            orderPayCallBackBean.setPayTime("2015-09-09T22:53:22");
            orderPayCallBackBean.setSignMethod("");
            orderPayCallBackBean.setTraceId(trackId);
            orderPayCallBackBean.setInstPaymentId("radamInstPaymentId");
            orderPayCallBackBean.setInternalUserId("internaluseid");
            orderPayCallBackBean.setExternalUserId("radamuserId");
            orderPayCallBackBean.setPayChannel("Alipay");
            orderPayCallBackBean.setPayType("type");
            orderPayCallBackBean.setVersion(1);
            // 发送请求
            orderPostPayCallBackCall.setData(orderPayCallBackBean);
            orderPostPayCallBackCall.callService();
          
            Map act = new HashMap();
            act.put("bizno", String.valueOf(orderId1));
            act.put("userId", userId);         
    		List<YmtAccountEntry> actinfos = new AccountDefaultWapper().selectAccountEntryByBizNo(act);
    		//计算orderId1 支付+补尾款 资金流水金额
    		BigDecimal accountEntryPrice=actinfos.stream().map(a->a.getAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
    		            
            Logger.verifyEquals(orderCallService.getNeedPrePayAmountOfCash(orderId1).add(order1NeedPostPayAmout).setScale(2), accountEntryPrice.divide(new BigDecimal(-1),2,4), "验证账户资金流水金额");
             
            int orderId = orderId1;
            // Ymt_PostPayTradingInfo 表
            List<Map> ymtPostPayTading = database.selectPostPayTradingInfo(orderId);
            // 订单表
            List<Map> ymtOrders = database.selectOrderByOderId(orderId);
            // 订单资金表
            List<Map> ymtOrderStateList = database.selectOrderStateByOderId(orderId);
            // 当前子订单需要支付的金额
            needPayAmount = new OrderCallService().getNeedPrePayAmountOfCash(orderId);
            Logger.comment("验证Ymt_Orders表");
            Logger.verifyEquals(
                    17,
                    (Integer) ymtOrders.get(0).get("iTradingStatus"), "验证iTradingStatus");
            Logger.comment("验证ymt_orderstate表");
            Logger.verifyEquals(
                    order1NeedPostPayAmout,
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfCash"), "验证fPostPaidAmountOfCash");
            Logger.verifyEquals(
                    order1NeedPostPayAmout.subtract(postPayBlance),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfThirdParty"),
                    "验证fPostPaidAmountOfThirdParty");
            Logger.verifyEquals(
                    postPayBlance.setScale(2),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfYmtAccount"),
                    "验证fPostPaidAmountOfYmtAccount");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_OrderPostPayCallBack_004() {
        Logger.start(true, "一订单下多个商品,补款金额大于实际需要补款金额，支付网关回调，校验订单,商品调整金额分摊");
        try {
            DefaultWapper database = new DefaultWapper();
            OrderCallService orderCallService = new OrderCallService();
            // 创建订单
            Logger.comment("前置数据准备");
            Map map = placeOrder2();
            Logger.comment("前置数据准备结束");
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            int orderId = (Integer) map.get("orderId");
            int tradingId = (Integer) map.get("tradingId");
            // 获取用户需要支付的金额
            BigDecimal needPayAmount = orderCallService.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
            // 13 为 app国际支付宝，15 微信
            String payType = "15";
            orderCallService.orderPayRequestInJava(userId, needPayAmount, tradingId, payType);
            // 卖家接单
            orderCallService.acceptOrder(orderId, sellerId);
            // 订单1 需要补款金额
            BigDecimal order1NeedPostPayAmout =
                    orderCallService.getPayableAmount(orderId).subtract(
                            orderCallService.getNeedPrePayAmountOfCash(orderId));
            // 实际补款金额
            BigDecimal order1ActPostPayAmout = order1NeedPostPayAmout.add(new BigDecimal(86));
            // 申请补款1
            orderCallService.applyForPostPay(orderId, sellerId, order1ActPostPayAmout);
            // 余额
            BigDecimal postPayBlance = new BigDecimal(9);
            // 补款
            orderCallService.orderPostPayRequest(userId, postPayBlance, orderId,
                    payType);
            // 第三方补款交易号
            List<Map> thirdPostPayTradingInfo = new DefaultWapper().selectPostPayTradingInfo(orderId);
            String thirdPostPayTradingId = (String) thirdPostPayTradingInfo.get(0).get("PostPayTradingId");
            // 支付网关支付回调交易
            PaymentProxyCallService.notifyCall(payType, thirdPostPayTradingId,
                    order1ActPostPayAmout.subtract(postPayBlance).toString()
                    , userId);
            Thread.sleep(5000);
            // 支付后金额
            Map act = new HashMap();
            act.put("bizno", String.valueOf(orderId));
            act.put("userId", userId);         
    		List<YmtAccountEntry> actinfos = new AccountDefaultWapper().selectAccountEntryByBizNo(act);
    		//计算orderId1 支付+补尾款 资金流水金额
    		BigDecimal accountEntryPrice=actinfos.stream().map(a->a.getAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
    		            
            Logger.verifyEquals(orderCallService.getNeedPrePayAmountOfCash(orderId).add(order1NeedPostPayAmout).setScale(2), accountEntryPrice.divide(new BigDecimal(-1),2,4), "验证账户资金流水金额");
       
            // 订单表
            List<Map> ymtOrders = database.selectOrderByOderId(orderId);
            // 订单商品明细
            List<Map> ymtOrdersInfoList = database.selectOrderInfoByOderId(orderId);
            // 订单资金表
            List<Map> ymtOrderStateList = database.selectOrderStateByOderId(orderId);
            // 当前子订单需要支付的金额
            needPayAmount = new OrderCallService().getNeedPrePayAmountOfCash(orderId);
            Logger.comment("验证Ymt_Orders表");
            Logger.verifyEquals(
                    17,
                    (Integer) ymtOrders.get(0).get("iTradingStatus"), "验证iTradingStatus");
            // 订单调整金额
            BigDecimal discount = order1ActPostPayAmout.subtract(order1NeedPostPayAmout).setScale(2);
            Logger.verifyEquals(
                    discount,
                    (BigDecimal) ymtOrders.get(0).get("fDiscount"),
                    "验证fDiscount");

            BigDecimal newPayableAmount = orderCallService.getPayableAmount(orderId);

            // 应付金额调整
            Logger.verifyEquals(
                    newPayableAmount,
                    (BigDecimal) ymtOrders.get(0).get("fPayableAmount"),
                    "验证fPayableAmount");

            Logger.comment("验证ymt_orderstate表");
            Logger.verifyEquals(
                    order1ActPostPayAmout,
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfCash"), "验证fPostPaidAmountOfCash");
            Logger.verifyEquals(
                    order1ActPostPayAmout.subtract(postPayBlance),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfThirdParty"),
                    "验证fPostPaidAmountOfThirdParty");
            Logger.verifyEquals(
                    postPayBlance.setScale(2),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfYmtAccount"),
                    "验证fPostPaidAmountOfYmtAccount");
            // 订单成交价
            BigDecimal totalPrice = (BigDecimal) ymtOrders.get(0).get("fTotalPrice");
            BigDecimal tempDicount = BigDecimal.ZERO;
            Logger.comment("验证调整金额分摊到商品上");
            for (int i = 0; i < ymtOrdersInfoList.size(); i++) {
                // 订单商品信息表
                Map orderInfoMap = ymtOrdersInfoList.get(i);
                BigDecimal productPrice = (BigDecimal) orderInfoMap.get("fProductPrice");
                int proNum = (Integer) orderInfoMap.get("iAmount");

                BigDecimal rate = productPrice.multiply(new BigDecimal(proNum))
                        .divide(totalPrice, 5, BigDecimal.ROUND_HALF_DOWN);
                BigDecimal productDiscount = new BigDecimal(0);
                BigDecimal productFreight = new BigDecimal(0);
                if (i == ymtOrdersInfoList.size() - 1) {
                    productDiscount = discount.subtract(tempDicount);
                }
                else {
                    // 商品里的调整分摊金额
                    productDiscount =
                            discount.multiply(productPrice.multiply(new BigDecimal(proNum))).
                                    divide(totalPrice, 2, BigDecimal.ROUND_HALF_DOWN);
                }
                tempDicount = tempDicount.add(productDiscount);
                Logger.verifyEquals(
                        productDiscount,
                        (BigDecimal) orderInfoMap.get("fDiscount"),
                        "验证fDiscount");
            }
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_OrderPostPayCallBack_005() {
        Logger.start(true, "一订单下多个商品,补款金额小于实际需要补款金额，支付网关回调，校验订单,商品调整金额分摊");
        try {
            DefaultWapper database = new DefaultWapper();
            OrderCallService orderCallService = new OrderCallService();
            // 创建订单
            Logger.comment("前置数据准备");
            Map map = placeOrder2();
            Logger.comment("前置数据准备结束");
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            int orderId = (Integer) map.get("orderId");
            int tradingId = (Integer) map.get("tradingId");
            // 获取用户需要支付的金额
            BigDecimal needPayAmount = orderCallService.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
            // 全款第三方支付
            // 13 为 app国际支付宝，15 微信
            String payType = "15";
            orderCallService.orderPayRequestInJava(userId, needPayAmount, tradingId, payType);
            // 卖家接单
            orderCallService.acceptOrder(orderId, sellerId);
            // 订单1 需要补款金额
            BigDecimal order1NeedPostPayAmout =
                    orderCallService.getPayableAmount(orderId).subtract(
                            orderCallService.getNeedPrePayAmountOfCash(orderId));
            // 实际补款金额
            BigDecimal order1ActPostPayAmout = order1NeedPostPayAmout.add(new BigDecimal(-16));
            // 申请补款1
            orderCallService.applyForPostPay(orderId, sellerId, order1ActPostPayAmout);
            // 余额
            BigDecimal postPayBlance = new BigDecimal(9);
            // 补款
            orderCallService.orderPostPayRequest(userId, postPayBlance, orderId,
                    payType);
            // 第三方补款交易号
            List<Map> thirdPostPayTradingInfo = new DefaultWapper().selectPostPayTradingInfo(orderId);
            String thirdPostPayTradingId = (String) thirdPostPayTradingInfo.get(0).get("PostPayTradingId");
            // 支付网关支付回调交易
            PaymentProxyCallService.notifyCall(payType, thirdPostPayTradingId,
                    order1ActPostPayAmout.subtract(postPayBlance).toString()
                    , userId);
            Thread.sleep(5000);
            // 支付后金额
            Map act = new HashMap();
            act.put("bizno", String.valueOf(orderId));
            act.put("userId", userId);         
    		List<YmtAccountEntry> actinfos = new AccountDefaultWapper().selectAccountEntryByBizNo(act);
    		//计算orderId1 支付+补尾款 资金流水金额
    		BigDecimal accountEntryPrice=actinfos.stream().map(a->a.getAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
    		            
            Logger.verifyEquals(orderCallService.getNeedPrePayAmountOfCash(orderId).add(order1NeedPostPayAmout).setScale(2), accountEntryPrice.divide(new BigDecimal(-1),2,4), "验证账户资金流水金额");
       
            // 订单表
            List<Map> ymtOrders = database.selectOrderByOderId(orderId);
            // 订单商品明细
            List<Map> ymtOrdersInfoList = database.selectOrderInfoByOderId(orderId);
            // 订单资金表
            List<Map> ymtOrderStateList = database.selectOrderStateByOderId(orderId);
            // 当前子订单需要支付的金额
            needPayAmount = new OrderCallService().getNeedPrePayAmountOfCash(orderId);
            Logger.comment("验证Ymt_Orders表");
            Logger.verifyEquals(
                    17,
                    (Integer) ymtOrders.get(0).get("iTradingStatus"), "验证iTradingStatus");
            // 订单调整金额
            BigDecimal discount = order1ActPostPayAmout.subtract(order1NeedPostPayAmout).setScale(2);
            Logger.verifyEquals(
                    discount,
                    (BigDecimal) ymtOrders.get(0).get("fDiscount"),
                    "验证fDiscount");

            BigDecimal newPayableAmount = orderCallService.getPayableAmount(orderId);

            // 应付金额调整
            Logger.verifyEquals(
                    newPayableAmount,
                    (BigDecimal) ymtOrders.get(0).get("fPayableAmount"),
                    "验证fPayableAmount");

            Logger.comment("验证ymt_orderstate表");
            Logger.verifyEquals(
                    order1ActPostPayAmout,
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfCash"), "验证fPostPaidAmountOfCash");
            Logger.verifyEquals(
                    order1ActPostPayAmout.subtract(postPayBlance),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfThirdParty"),
                    "验证fPostPaidAmountOfThirdParty");
            Logger.verifyEquals(
                    postPayBlance.setScale(2),
                    (BigDecimal) ymtOrderStateList.get(0).get("fPostPaidAmountOfYmtAccount"),
                    "验证fPostPaidAmountOfYmtAccount");
            // 订单成交价
            BigDecimal totalPrice = (BigDecimal) ymtOrders.get(0).get("fTotalPrice");
            BigDecimal tempDicount = BigDecimal.ZERO;
            Logger.comment("验证调整金额分摊到商品上");
            for (int i = 0; i < ymtOrdersInfoList.size(); i++) {
                // 订单商品信息表
                Map orderInfoMap = ymtOrdersInfoList.get(i);
                BigDecimal productPrice = (BigDecimal) orderInfoMap.get("fProductPrice");
                int proNum = (Integer) orderInfoMap.get("iAmount");

                BigDecimal rate = productPrice.multiply(new BigDecimal(proNum))
                        .divide(totalPrice, 5, BigDecimal.ROUND_HALF_UP);
                BigDecimal productDiscount = new BigDecimal(0);
                BigDecimal productFreight = new BigDecimal(0);
                if (i == ymtOrdersInfoList.size() - 1) {
                    productDiscount = discount.subtract(tempDicount);
                }
                else {
                    // 商品里的调整分摊金额
                    productDiscount =
                            discount.multiply(productPrice.multiply(new BigDecimal(proNum))).
                                    divide(totalPrice, 2, BigDecimal.ROUND_HALF_UP);
                }
                tempDicount = tempDicount.add(productDiscount);
                Logger.verifyEquals(
                        productDiscount,
                        (BigDecimal) orderInfoMap.get("fDiscount"),
                        "验证fDiscount");
            }
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    // 校验第三方支付通知
    private void verifyDb(int tradingId, List<Integer> orderIds, BigDecimal needPayAmount, BigDecimal thirdPayAmount) {
        DefaultWapper database = new DefaultWapper();
        for (int i = 0; i < orderIds.size(); i++) {
            int orderId = orderIds.get(i);
            // 订单主表
            List<Map> ymtOrders = database.selectOrderByOderId(orderId);
            // 订单商品明细
            List<Map> ymtOrdersInfoList = database.selectOrderInfoByOderId(orderId);
            // ymtOrderState 表
            List<Map> ymtOrderState = database.selectOrderStateByOderId(orderId);
            // ymt_orderpay 表
            List<Map> ymtOrderPay = database.selectOrderPay(orderId);
            // Ymt_OrderPostPay 表
            List<Map> ymtOrderPostPay = database.selectOrderPostPayByOrderId(orderId);
            // 优惠券金额
            BigDecimal couponValue = (BigDecimal) ymtOrders.get(0).get("CouponValue");
            // 商家用户优惠券金额
            BigDecimal sellerCouponValue = (BigDecimal) ymtOrders.get(0).get("fSellerCouponAmount");
            // 平台用户优惠券金额
            BigDecimal ymtCouponValue = (BigDecimal) ymtOrders.get(0).get("fYmtCouponAmount");
            // 查询当前订单需要支付的定金
            BigDecimal orderNeedPayAmount = new OrderCallService().getNeedPrePayAmountOfCash(orderId);

            BigDecimal dtemp = new BigDecimal(0);
            BigDecimal orderThirdParty = new BigDecimal(0);
            BigDecimal rate = orderNeedPayAmount.divide(needPayAmount, 5, BigDecimal.ROUND_HALF_DOWN);
            Logger.debug("rate:" + rate);

            if (i == orderIds.size()) {
                orderThirdParty = thirdPayAmount.subtract(dtemp);
            }
            else {
                orderThirdParty = rate.multiply(thirdPayAmount).divide(new BigDecimal(1), 2, BigDecimal.ROUND_HALF_UP);
            }

            Logger.comment("验证订单号:" + orderId);
            Logger.comment("验证ymt_order表");
            Logger.verifyEquals(
                    2,
                    (Integer) ymtOrders.get(0).get("iTradingStatus"), "验证iTradingStatus");
            Logger.comment("验证ymt_orderState表");
            Logger.verifyEquals(1, ymtOrderState.size(), "验证ymt_orderState表");
            Logger.verifyEquals(
                    couponValue,
                    (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfCoupon"), "验证fPaidAmountOfCoupon");
            // 余额+第三方支付
            Logger.verifyEquals(
                    orderNeedPayAmount,
                    (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfCash"), "验证fPaidAmountOfCash");
            Logger.verifyEquals(
                    sellerCouponValue,
                    (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfSellerCoupon"), "验证fPaidAmountOfSellerCoupon");
            Logger.verifyEquals(
                    ymtCouponValue,
                    (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfYmtCoupon"), "验证fPaidAmountOfYmtCoupon");

            Logger.verifyEquals(
                    orderNeedPayAmount.subtract(orderThirdParty),
                    (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfYmtAccount"), "验证fPaidAmountOfYmtAccount");
            Logger.verifyEquals(
                    orderThirdParty,
                    (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfThirdParty"), "验证fPaidAmountOfThirdParty");

            Logger.comment("验证ymt_orderpay表");
            Logger.verifyEquals(
                    tradingId,
                    (Integer) ymtOrderPay.get(0).get("iTradingId"), "验证iTradingId");
            Logger.verifyEquals(
                    2,
                    (Integer) ymtOrderPay.get(0).get("iPayType"), "验证iPayType");
            Logger.verifyEquals(
                    orderNeedPayAmount,
                    (BigDecimal) ymtOrderPay.get(0).get("fAmount"), "验证fAmount");
            Logger.verifyEquals(
                    0,
                    (Integer) ymtOrderPay.get(0).get("iStatus"), "验证iStatus");
            Logger.verifyEquals(
                    "支付网关回调处理成功",
                    (String) ymtOrderPay.get(0).get("sMemo"), "验证sMemo");
            Logger.verifyEquals(
                    1,
                    (Integer) ymtOrderPay.get(0).get("iAccountingNum"), "验证iAccountingNum");
            Logger.verifyEquals(
                    "操作成功！",
                    (String) ymtOrderPay.get(0).get("sAccountingMessage"), "验证sAccountingMessage");
            dtemp = dtemp.add(orderThirdParty);

        }

        // ymt_tradinginfo 表
        List<Map> ymtTradinginfo = database.selectTradinginfoByTradingId(tradingId);
        Logger.comment("验证ymt_tradinginfo表");
        Logger.verifyEquals(
                thirdPayAmount.setScale(2),
                (BigDecimal) ymtTradinginfo.get(0).get("fAmount"), "验证fAmount");
        Logger.verifyEquals(
                2,
                (Integer) ymtTradinginfo.get(0).get("iTradingStatus"), "验证iTradingStatus");
        Logger.verifyEquals(
                needPayAmount,
                (BigDecimal) ymtTradinginfo.get(0).get("fPayableAmount"), "验证fPayableAmount");
    }

    // 校验第三方补款通知
    private void verifyDb2(int tradingId, int orderId, BigDecimal payAmount, BigDecimal thirdPayAmount) {
        // 用户支付的余额
        DefaultWapper database = new DefaultWapper();
        // 订单主表
        List<Map> ymtOrders = database.selectOrderByOderId(orderId);
        // 订单商品明细
        List<Map> ymtOrdersInfoList = database.selectOrderInfoByOderId(orderId);
        // ymt_tradinginfo 表
        List<Map> ymtTradinginfo = database.selectTradinginfoByTradingId(tradingId);
        // ymtOrderState 表
        List<Map> ymtOrderState = database.selectOrderStateByOderId(orderId);
        // Ymt_OrderPostPay 表
        List<Map> ymtOrderPostPay = database.selectOrderPostPayByOrderId(orderId);
        // 需要支付的定金
        BigDecimal needPayAmount1 = new OrderCallService().getNeedPrePayAmountOfCash(orderId);
        // 优惠券金额
        BigDecimal couponValue = (BigDecimal) ymtOrders.get(0).get("CouponValue");
        // 商家用户优惠券金额
        BigDecimal sellerCouponValue = (BigDecimal) ymtOrders.get(0).get("fSellerCouponAmount");
        // 平台用户优惠券金额
        BigDecimal ymtCouponValue = (BigDecimal) ymtOrders.get(0).get("fYmtCouponAmount");

        Logger.comment("验证订单号:" + orderId);
        Logger.comment("验证ymt_order表");
        Logger.verifyEquals(
                17,
                (Integer) ymtOrders.get(0).get("iTradingStatus"), "验证iTradingStatus");

        Logger.comment("验证ymt_orderState表");
        Logger.verifyEquals(1, ymtOrderState.size(), "验证ymt_orderState表");
        Logger.verifyEquals(
                couponValue,
                (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfCoupon"), "验证fPaidAmountOfCoupon");
        // 补款支付总额
        Logger.verifyEquals(
                payAmount.add(thirdPayAmount),
                (BigDecimal) ymtOrderState.get(0).get("fPostPaidAmountOfCash"), "验证fPostPaidAmountOfCash");
        Logger.verifyEquals(
                sellerCouponValue,
                (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfSellerCoupon"), "验证fPaidAmountOfSellerCoupon");
        Logger.verifyEquals(
                ymtCouponValue,
                (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfYmtCoupon"), "验证fPaidAmountOfYmtCoupon");
        // 定金付款ymt余额
        Logger.verifyEquals(
                needPayAmount1,
                (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfYmtAccount"), "验证fPaidAmountOfYmtAccount");
        // 定金付款第三方金额
        Logger.verifyEquals(
                BigDecimal.ZERO.setScale(2),
                (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfThirdParty"), "验证fPaidAmountOfThirdParty");
        // 补款付款ymt余额
        Logger.verifyEquals(
                payAmount,
                (BigDecimal) ymtOrderState.get(0).get("fPostPaidAmountOfYmtAccount"), "验证fPostPaidAmountOfYmtAccount");
        // 补款付款第三方金额
        Logger.verifyEquals(
                thirdPayAmount.setScale(2),
                (BigDecimal) ymtOrderState.get(0).get("fPostPaidAmountOfThirdParty"), "验证fPostPaidAmountOfThirdParty");

    }


    // 一次下单生成2个订单号,分别使用平台,商家优惠券,每个订单下面3个商品
    private Map placeOrder() {
        Map map = new HashMap();
        try {
            PlaceOrderBean placeorderBean = new PlaceOrderBean();
            PlaceOrderCall placeorderCall = new PlaceOrderCall();
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            // 创建产品
            Logger.comment("前置数据准备");
            HashMap<String, Object> productInfo = ProductCallService.createProduct(sellerId, 3);
            Logger.comment("前置数据准备结束");
            String productId = (String) productInfo.get("productId");
            Logger.debug("productId:" + productId);
            String productName = (String) productInfo.get("productName");
            String pic = (String) productInfo.get("pic");
            BigDecimal price = (BigDecimal) (productInfo.get("price"));
            // String catalogId = (String) productInfo.get("catalogId");
            // 买家用户等级
            int userLevel = 3;
            // 订单来源
            int orderSource = OrderSourceEnum.APP.getValue();
            placeorderBean.setOrderSource(orderSource);
            // 终端来源
            int terminalSource = TerminalSourceEnum.IOS.getValue();
            placeorderBean.setTerminalSource(terminalSource);
            // 应用终端来源
            int appTerminalSource = AppTerminalSourceEnum.Phone.getValue();
            placeorderBean.setAppTerminalSource(appTerminalSource);
            // SourceIp
            String sourceIp = "127.0.0.1";
            placeorderBean.setSourceIp(sourceIp);
            // 用户设备号
            String deviceId = "";
            placeorderBean.setDeviceId(deviceId);
            // 订单信息List
            List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
            OrderInfo order = new OrderInfo();
            // 订单类型
            int orderType = OrderTypeEnum.Shangou.getValue();
            order.setOrderType(orderType);
            // 卖家
            order.setSellerId(sellerId);
            String leaveWord = "autotest";
            order.setLeaveWord(leaveWord);
            // 运费
            BigDecimal freight = new BigDecimal(23);
            order.setFreight(freight);
            // 优惠券
            // 领用优惠券
            BigDecimal couponValue = new BigDecimal(10);
            // 领用商家优惠券
            String couponCode = new CouponCallService().receiveCouponInfo(sellerId, userId, couponValue);
            order.setCouponCode(couponCode);
            // 优惠券类型
            int couponType = 1;
            order.setCouponUseType(couponType);
            // 优惠券渠道
            int couponChannel = 2;
            order.setCouponChannel(couponChannel);
            // 优惠券金额
            order.setCouponValue(couponValue);
            // 是否全额付款
            boolean isPaidFull = false;
            order.setIsPaidFull(isPaidFull);
            // 收货地址
            String address = "上海市闸北区灵石路636号";
            order.setAddress(address);
            // 邮编
            String postCode = "200001";
            order.setPostCode(postCode);
            // 收件人姓名
            String receieveName = "李四";
            order.setReceieveName(receieveName);
            // 收货人手机号
            String phone = "13100000001";
            order.setPhone(phone);
            // 收货人电话
            String telephone = "021-51002100";
            order.setTelephone(telephone);
            // 自动取消小时数
            BigDecimal autoCancelOrderHours = new BigDecimal(10);
            order.setAutoCancelOrderHours(autoCancelOrderHours);
            // 订单商品信息
            List<OrderProductInfo> orderProductInfoList = new ArrayList<OrderProductInfo>();
            OrderProductInfo productinfo = new OrderProductInfo();
            OrderProductInfo productinfo2 = new OrderProductInfo();
            OrderProductInfo productinfo3 = new OrderProductInfo();
            // 商品id
            productinfo.setProductId(productId);
            // 商品名称
            productinfo.setProductName(productName);
            // 商品规格id
            String catalogId = TestDataManager.getRandomUUID().toString();
            productinfo.setCatalogId(catalogId);
            // 备货状态(1-现货 2-代购 3-FBX)
            int catalogType = StockStatusEnum.Spot.getValue();
            productinfo.setStockStatusEnum(catalogType);
            // 商品数量
            int productNum = 1 + new Random().nextInt(5);
            productinfo.setProductNum(productNum);
            // 商品一级目录id
            int firstCategoryId = 1;
            productinfo.setFirstCategoryId(firstCategoryId);
            // 商品一级目录id
            int secondCategoryId = 2;
            productinfo.setSecondCategoryId(secondCategoryId);
            // 商品一级目录id
            int thirdCategoryId = 3;
            productinfo.setThirdCategoryId(thirdCategoryId);
            // 商品品牌id
            int brandId = 5;
            productinfo.setBrandId(brandId);
            // 商品url
            productinfo.setPictureUrl(pic);
            // 商品属性信息(按JSON格式存储，如--颜色:黑色,尺寸:XXS)
            String propetyInfo = "";
            productinfo.setPropetyInfo(propetyInfo);
            // 商品价格
            BigDecimal productPrice = price;
            productinfo.setProductPrice(productPrice);
            // 商品预付定金价格
            BigDecimal earnestPrice = price.divide(new BigDecimal(3), 2, BigDecimal.ROUND_HALF_UP); // 小数点后保留2位并四舍五入
            productinfo.setEarnestPrice(earnestPrice);
            // 价格类型
            int priceType = PriceTypeEnum.Unkown.getValue();
            productinfo.setPriceType(priceType);
            // 买家或卖家承担关税(0-卖家承担 1-买家承担)
            int tariffType = 0;
            productinfo.setTariffType(tariffType);
            // 物流类型
            int logisticsType = LogisticsTypeEnum.SailProtect.getValue();
            productinfo.setLogisticsType(logisticsType);
            // 保税区
            int bondedArea = BondedAreaEnum.NingBo.getValue();
            productinfo.setBondedArea(bondedArea);
            // 商品备案号
            String productRecordNo = "123456";
            productinfo.setProductRecordNo(productRecordNo);
            // SKU
            String sku = "1234567";
            productinfo.setSKU(sku);
            // 套餐编号
            String packageNo = "11111111";
            productinfo.setPackageNo(packageNo);
            // 商品退货渠道
            int productRefundChannel = ProductRefundChannelEnum.Official.getValue();
            productinfo.setProductRefundChannel(productRefundChannel);
            // 商品信息(存储格式为：Json格式)
            String productDetailInfo =
                    "e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男";
            productinfo.setProductDetailInfo(productDetailInfo);
            // 活动商品信息
            ProductActivityInfo activityInfo = new ProductActivityInfo();
            // 商品活动号
            int activityId = 46870;
            activityInfo.setActivityId(activityId);
            // 额外费用
            BigDecimal premium = new BigDecimal(10);
            activityInfo.setPremium(premium);
            // 是否算入成本价
            boolean isCost = false;
            activityInfo.setIsCost(isCost);
            // 是否使用优惠券
            boolean isUseCoupon = false;
            activityInfo.setIsUseCoupon(isUseCoupon);
            // 是否免佣金
            boolean isFeeFree = false;
            activityInfo.setIsFeeFree(isFeeFree);
            // 活动开始时间
            String beginDate = YMTDateUtil.getBeforeOrNextDay(-100);
            activityInfo.setActivityBeginDate(beginDate);
            // 活动结束时间
            String endDate = YMTDateUtil.getBeforeOrNextDay(10);
            activityInfo.setActivityEndDate(endDate);
            // 商品活动模板号
            int activityTemplateId = 987654;
            activityInfo.setActivityTemplateId(activityTemplateId);
            // 商品活动促销类型
            int promotionType = ActivityPromotionTypeEnum.DiscountPrice.getValue();
            activityInfo.setPromotionType(promotionType);
            // 促销价格
            BigDecimal promotionPrice = new BigDecimal(1);
            activityInfo.setPromotionPrice(promotionPrice);
            // 促销价格
            BigDecimal commissionRate = new BigDecimal(0);
            activityInfo.setCommissionRate(commissionRate);
            productinfo.setActivityInfo(activityInfo);
            // 第二件商品
            // 商品id
            productinfo2.setProductId(productId);
            // 商品名称
            productinfo2.setProductName(productName);
            // 商品规格id
            String catalogId2 = TestDataManager.getRandomUUID().toString();
            productinfo2.setCatalogId(catalogId2);
            // 备货状态(1-现货 2-代购 3-FBX)
            int catalogType2 = StockStatusEnum.Spot.getValue();
            productinfo2.setStockStatusEnum(catalogType2);
            // 商品数量
            int productNum2 = 1 + new Random().nextInt(5);
            productinfo2.setProductNum(productNum2);
            // 商品一级目录id
            int firstCategoryId2 = 1;
            productinfo2.setFirstCategoryId(firstCategoryId2);
            // 商品一级目录id
            int secondCategoryId2 = 2;
            productinfo2.setSecondCategoryId(secondCategoryId2);
            // 商品一级目录id
            int thirdCategoryId2 = 3;
            productinfo2.setThirdCategoryId(thirdCategoryId2);
            // 商品品牌id
            int brandId2 = 3;
            productinfo2.setBrandId(brandId2);
            // 商品url
            productinfo2.setPictureUrl(pic);
            // 商品属性信息(按JSON格式存储，如--颜色:黑色,尺寸:XXS)
            String propetyInfo2 = "";
            productinfo2.setPropetyInfo(propetyInfo2);
            // 商品价格
            BigDecimal productPrice2 = price;
            productinfo2.setProductPrice(productPrice2);
            // 商品预付定金价格
            BigDecimal earnestPrice2 = price.divide(new BigDecimal(3), 2, BigDecimal.ROUND_HALF_UP); // 小数点后保留2位并四舍五入
            productinfo2.setEarnestPrice(earnestPrice2);
            // 价格类型
            int priceType2 = PriceTypeEnum.NewPersonPrice.getValue();
            productinfo2.setPriceType(priceType2);
            // 买家或卖家承担关税(0-卖家承担 1-买家承担)
            int tariffType2 = 0;
            productinfo2.setTariffType(tariffType2);
            // 物流类型
            int logisticsType2 = LogisticsTypeEnum.XloboBondedDelivery.getValue();
            productinfo2.setLogisticsType(logisticsType2);
            // 保税区
            int bondedArea2 = BondedAreaEnum.NingBo.getValue();
            productinfo2.setBondedArea(bondedArea2);
            // 商品备案号
            String productRecordNo2 = "123456";
            productinfo2.setProductRecordNo(productRecordNo2);
            // SKU
            String sku2 = "1234567";
            productinfo2.setSKU(sku2);
            // 套餐编号
            String packageNo2 = "11111111";
            productinfo2.setPackageNo(packageNo2);
            // 商品退货渠道
            int productRefundChannel2 = ProductRefundChannelEnum.Official.getValue();
            productinfo2.setProductRefundChannel(productRefundChannel2);
            // 商品信息(存储格式为：Json格式)
            String productDetailInfo2 =
                    "e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男";
            productinfo2.setProductDetailInfo(productDetailInfo2);
            productinfo2.setActivityInfo(activityInfo);
            // 第三件商品
            // 商品id
            productinfo3.setProductId(productId);
            // 商品名称
            productinfo3.setProductName(productName);
            // 商品规格id
            String catalogId3 = TestDataManager.getRandomUUID().toString();
            productinfo3.setCatalogId(catalogId3);
            // 备货状态(1-现货 2-代购 3-FBX)
            productinfo3.setStockStatusEnum(catalogType);
            // 商品数量
            int productNum3 = 1 + new Random().nextInt(5);
            productinfo3.setProductNum(productNum3);
            // 商品一级目录id
            productinfo3.setFirstCategoryId(firstCategoryId);
            // 商品一级目录id
            productinfo3.setSecondCategoryId(secondCategoryId);
            // 商品一级目录id
            productinfo3.setThirdCategoryId(thirdCategoryId);
            // 商品品牌id
            int brandId3 = 3;
            productinfo3.setBrandId(brandId3);
            // 商品url
            productinfo3.setPictureUrl(pic);
            // 商品属性信息(按JSON格式存储，如--颜色:黑色,尺寸:XXS)
            String propetyInfo3 = "";
            productinfo3.setPropetyInfo(propetyInfo3);
            // 商品价格
            BigDecimal productPrice3 = price;
            productinfo3.setProductPrice(productPrice3);
            // 商品预付定金价格
            BigDecimal earnestPrice3 = price.divide(new BigDecimal(3), 2, BigDecimal.ROUND_HALF_UP); // 小数点后保留2位并四舍五入
            productinfo3.setEarnestPrice(earnestPrice3);
            // 价格类型
            int priceType3 = PriceTypeEnum.NewPersonPrice.getValue();
            productinfo3.setPriceType(priceType3);
            // 买家或卖家承担关税(0-卖家承担 1-买家承担)
            int tariffType3 = 0;
            productinfo3.setTariffType(tariffType3);
            // 物流类型
            int logisticsType3 = LogisticsTypeEnum.XloboBondedDelivery.getValue();
            productinfo3.setLogisticsType(logisticsType3);
            // 保税区
            int bondedArea3 = BondedAreaEnum.NingBo.getValue();
            productinfo3.setBondedArea(bondedArea3);
            // 商品备案号
            productinfo3.setProductRecordNo(productRecordNo);
            // SKU
            productinfo3.setSKU(sku);
            // 套餐编号
            productinfo3.setPackageNo(packageNo);
            // 商品退货渠道
            productinfo3.setProductRefundChannel(productRefundChannel);
            // 商品信息(存储格式为：Json格式)
            productinfo3.setProductDetailInfo(productDetailInfo);
            // 活动商品信息
            productinfo3.setActivityInfo(activityInfo);

            orderProductInfoList.add(productinfo);
            orderProductInfoList.add(productinfo2);
            orderProductInfoList.add(productinfo3);
            // 订单商品信息
            order.setOrderProductInfoList(orderProductInfoList);
            // 第二个订单
            OrderInfo order2 = new OrderInfo();
            // 订单类型
            int orderType2 = OrderTypeEnum.Shangou.getValue();
            order2.setOrderType(orderType2);
            // 卖家
            order2.setSellerId(sellerId);
            String leaveWord2 = "autotest";
            order2.setLeaveWord(leaveWord2);
            // 运费
            BigDecimal freight2 = new BigDecimal(20);
            order2.setFreight(freight2);
            // 优惠券
            BigDecimal couponValue2 = new BigDecimal(11);
            // ymt 平台优惠券
            String couponCode2 = new OrderCallService().receivePlatformCoupon(userId, couponValue);
            order2.setCouponCode(couponCode2);
            // 优惠券类型
            int couponType2 = 1;
            order2.setCouponUseType(couponType2);
            // 优惠券渠道 1 ymt平台，2 商家
            int couponChannel2 = 1;
            order2.setCouponChannel(couponChannel2);
            // 优惠券金额
            order2.setCouponValue(couponValue2);
            // 是否全额付款
            boolean isPaidFull2 = false;
            order2.setIsPaidFull(isPaidFull2);
            // 收货地址
            String address2 = "上海市闸北区灵石路636号";
            order2.setAddress(address2);
            // 邮编
            String postCode2 = "200001";
            order2.setPostCode(postCode2);
            // 收件人姓名
            String receieveName2 = "李四";
            order2.setReceieveName(receieveName2);
            // 收货人手机号
            String phone2 = "13100000002";
            order2.setPhone(phone2);
            // 收货人电话
            String telephone2 = "021-51002111";
            order2.setTelephone(telephone2);
            // 自动取消小时数
            BigDecimal autoCancelOrderHours2 = new BigDecimal(2);
            order2.setAutoCancelOrderHours(autoCancelOrderHours2);
            // 订单商品信息 拷贝第一单3个商品
            order2.setOrderProductInfoList(orderProductInfoList);
            // 订单信息
            orderInfoList.add(order);
            // 订单信息
            orderInfoList.add(order2);
            placeorderBean.setUserId(userId);
            placeorderBean.setOrderInfoList(orderInfoList);
            placeorderCall.setData(placeorderBean);
            placeorderCall.callService();
            JSONArray orders = placeorderCall.getOrderIds();
            int tradingId = placeorderCall.getTradingId();
            map.put("orders", orders);
            map.put("tradingId", tradingId);
        } catch (Exception e) {
            Logger.fail(e);
        }
        return map;
    }

    // 一次下单生成1个M订单，下面有3个商品
    private Map placeOrder2() {
        Map map = new HashMap();
        try {
            PlaceOrderBean placeorderBean = new PlaceOrderBean();
            PlaceOrderCall placeorderCall = new PlaceOrderCall();
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            // 创建产品
            Logger.comment("前置数据准备");
            HashMap<String, Object> productInfo = ProductCallService.createProduct(sellerId, 3);
            Logger.comment("前置数据准备结束");
            String productId = (String) productInfo.get("productId");
            Logger.debug("productId:" + productId);
            String productName = (String) productInfo.get("productName");
            String pic = (String) productInfo.get("pic");
            BigDecimal price = (BigDecimal) (productInfo.get("price"));
            // String catalogId = (String) productInfo.get("catalogId");
            // 买家用户等级
            int userLevel = 3;
            // 订单来源
            int orderSource = OrderSourceEnum.APP.getValue();
            placeorderBean.setOrderSource(orderSource);
            // 终端来源
            int terminalSource = TerminalSourceEnum.IOS.getValue();
            placeorderBean.setTerminalSource(terminalSource);
            // 应用终端来源
            int appTerminalSource = AppTerminalSourceEnum.Phone.getValue();
            placeorderBean.setAppTerminalSource(appTerminalSource);
            // SourceIp
            String sourceIp = "127.0.0.1";
            placeorderBean.setSourceIp(sourceIp);
            // 用户设备号
            String deviceId = "";
            placeorderBean.setDeviceId(deviceId);
            // 订单信息List
            List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
            OrderInfo order = new OrderInfo();
            // 订单类型
            int orderType = OrderTypeEnum.Shangou.getValue();
            order.setOrderType(orderType);
            // 卖家
            order.setSellerId(sellerId);
            String leaveWord = "autotest";
            order.setLeaveWord(leaveWord);
            // 运费
            BigDecimal freight = new BigDecimal(23);
            order.setFreight(freight);
            // 领用商家优惠券


            // 是否全额付款
            boolean isPaidFull = false;
            order.setIsPaidFull(isPaidFull);
            // 收货地址
            String address = "上海市闸北区灵石路636号";
            order.setAddress(address);
            // 邮编
            String postCode = "200001";
            order.setPostCode(postCode);
            // 收件人姓名
            String receieveName = "李四";
            order.setReceieveName(receieveName);
            // 收货人手机号
            String phone = "13100000001";
            order.setPhone(phone);
            // 收货人电话
            String telephone = "021-51002100";
            order.setTelephone(telephone);
            // 自动取消小时数
            BigDecimal autoCancelOrderHours = new BigDecimal(10);
            order.setAutoCancelOrderHours(autoCancelOrderHours);
            // 订单商品信息
            List<OrderProductInfo> orderProductInfoList = new ArrayList<OrderProductInfo>();
            OrderProductInfo productinfo = new OrderProductInfo();
            OrderProductInfo productinfo2 = new OrderProductInfo();
            OrderProductInfo productinfo3 = new OrderProductInfo();
            // 商品id
            productinfo.setProductId(productId);
            // 商品名称
            productinfo.setProductName(productName);
            // 商品规格id
            String catalogId = TestDataManager.getRandomUUID().toString();
            productinfo.setCatalogId(catalogId);
            // 备货状态(1-现货 2-代购 3-FBX)
            int catalogType = StockStatusEnum.Spot.getValue();
            productinfo.setStockStatusEnum(catalogType);
            // 商品数量
            int productNum = 1 + new Random().nextInt(5);
            productinfo.setProductNum(productNum);
            // 商品一级目录id
            int firstCategoryId = 1;
            productinfo.setFirstCategoryId(firstCategoryId);
            // 商品一级目录id
            int secondCategoryId = 2;
            productinfo.setSecondCategoryId(secondCategoryId);
            // 商品一级目录id
            int thirdCategoryId = 3;
            productinfo.setThirdCategoryId(thirdCategoryId);
            // 商品品牌id
            int brandId = 5;
            productinfo.setBrandId(brandId);
            // 商品url
            productinfo.setPictureUrl(pic);
            // 商品属性信息(按JSON格式存储，如--颜色:黑色,尺寸:XXS)
            String propetyInfo = "";
            productinfo.setPropetyInfo(propetyInfo);
            // 商品价格
            BigDecimal productPrice = new BigDecimal(110);
            productinfo.setProductPrice(productPrice);
            // 商品预付定金价格
            BigDecimal earnestPrice = new BigDecimal(26); // 小数点后保留2位并四舍五入
            productinfo.setEarnestPrice(earnestPrice);
            // 价格类型
            int priceType = PriceTypeEnum.Unkown.getValue();
            productinfo.setPriceType(priceType);
            // 买家或卖家承担关税(0-卖家承担 1-买家承担)
            int tariffType = 0;
            productinfo.setTariffType(tariffType);
            // 物流类型
            int logisticsType = LogisticsTypeEnum.SailProtect.getValue();
            productinfo.setLogisticsType(logisticsType);
            // 保税区
            int bondedArea = BondedAreaEnum.NingBo.getValue();
            productinfo.setBondedArea(bondedArea);
            // 商品备案号
            String productRecordNo = "123456";
            productinfo.setProductRecordNo(productRecordNo);
            // SKU
            String sku = "1234567";
            productinfo.setSKU(sku);
            // 套餐编号
            String packageNo = "11111111";
            productinfo.setPackageNo(packageNo);
            // 商品退货渠道
            int productRefundChannel = ProductRefundChannelEnum.Official.getValue();
            productinfo.setProductRefundChannel(productRefundChannel);
            // 商品信息(存储格式为：Json格式)
            String productDetailInfo =
                    "e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男";
            productinfo.setProductDetailInfo(productDetailInfo);
            // 活动商品信息
            ProductActivityInfo activityInfo = new ProductActivityInfo();
            // 商品活动号
            int activityId = 46870;
            activityInfo.setActivityId(activityId);
            // 额外费用
            BigDecimal premium = new BigDecimal(10);
            activityInfo.setPremium(premium);
            // 是否算入成本价
            boolean isCost = false;
            activityInfo.setIsCost(isCost);
            // 是否使用优惠券
            boolean isUseCoupon = false;
            activityInfo.setIsUseCoupon(isUseCoupon);
            // 是否免佣金
            boolean isFeeFree = false;
            activityInfo.setIsFeeFree(isFeeFree);
            // 活动开始时间
            String beginDate = YMTDateUtil.getBeforeOrNextDay(-100);
            activityInfo.setActivityBeginDate(beginDate);
            // 活动结束时间
            String endDate = YMTDateUtil.getBeforeOrNextDay(10);
            activityInfo.setActivityEndDate(endDate);
            // 商品活动模板号
            int activityTemplateId = 987654;
            activityInfo.setActivityTemplateId(activityTemplateId);
            // 商品活动促销类型
            int promotionType = ActivityPromotionTypeEnum.DiscountPrice.getValue();
            activityInfo.setPromotionType(promotionType);
            // 促销价格
            BigDecimal promotionPrice = new BigDecimal(1);
            activityInfo.setPromotionPrice(promotionPrice);
            // 促销价格
            BigDecimal commissionRate = new BigDecimal(0);
            activityInfo.setCommissionRate(commissionRate);
            productinfo.setActivityInfo(activityInfo);
            // 第二件商品
            // 商品id
            productinfo2.setProductId(productId);
            // 商品名称
            productinfo2.setProductName(productName);
            // 商品规格id
            String catalogId2 = TestDataManager.getRandomUUID().toString();
            productinfo2.setCatalogId(catalogId2);
            // 备货状态(1-现货 2-代购 3-FBX)
            int catalogType2 = StockStatusEnum.Spot.getValue();
            productinfo2.setStockStatusEnum(catalogType2);
            // 商品数量
            int productNum2 = 1 + new Random().nextInt(5);
            productinfo2.setProductNum(productNum2);
            // 商品一级目录id
            int firstCategoryId2 = 1;
            productinfo2.setFirstCategoryId(firstCategoryId2);
            // 商品一级目录id
            int secondCategoryId2 = 2;
            productinfo2.setSecondCategoryId(secondCategoryId2);
            // 商品一级目录id
            int thirdCategoryId2 = 3;
            productinfo2.setThirdCategoryId(thirdCategoryId2);
            // 商品品牌id
            int brandId2 = 3;
            productinfo2.setBrandId(brandId2);
            // 商品url
            productinfo2.setPictureUrl(pic);
            // 商品属性信息(按JSON格式存储，如--颜色:黑色,尺寸:XXS)
            String propetyInfo2 = "";
            productinfo2.setPropetyInfo(propetyInfo2);
            // 商品价格
            BigDecimal productPrice2 = new BigDecimal(126);
            productinfo2.setProductPrice(productPrice2);
            // 商品预付定金价格
            BigDecimal earnestPrice2 = new BigDecimal(35); // 小数点后保留2位并四舍五入
            productinfo2.setEarnestPrice(earnestPrice2);
            // 价格类型
            int priceType2 = PriceTypeEnum.NewPersonPrice.getValue();
            productinfo2.setPriceType(priceType2);
            // 买家或卖家承担关税(0-卖家承担 1-买家承担)
            int tariffType2 = 0;
            productinfo2.setTariffType(tariffType2);
            // 物流类型
            int logisticsType2 = LogisticsTypeEnum.XloboBondedDelivery.getValue();
            productinfo2.setLogisticsType(logisticsType2);
            // 保税区
            int bondedArea2 = BondedAreaEnum.NingBo.getValue();
            productinfo2.setBondedArea(bondedArea2);
            // 商品备案号
            String productRecordNo2 = "123456";
            productinfo2.setProductRecordNo(productRecordNo2);
            // SKU
            String sku2 = "1234567";
            productinfo2.setSKU(sku2);
            // 套餐编号
            String packageNo2 = "11111111";
            productinfo2.setPackageNo(packageNo2);
            // 商品退货渠道
            int productRefundChannel2 = ProductRefundChannelEnum.Official.getValue();
            productinfo2.setProductRefundChannel(productRefundChannel2);
            // 商品信息(存储格式为：Json格式)
            String productDetailInfo2 =
                    "e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男";
            productinfo2.setProductDetailInfo(productDetailInfo2);
            productinfo2.setActivityInfo(activityInfo);
            // 第三件商品
            // 商品id
            productinfo3.setProductId(productId);
            // 商品名称
            productinfo3.setProductName(productName);
            // 商品规格id
            String catalogId3 = TestDataManager.getRandomUUID().toString();
            productinfo3.setCatalogId(catalogId3);
            // 备货状态(1-现货 2-代购 3-FBX)
            productinfo3.setStockStatusEnum(catalogType);
            // 商品数量
            int productNum3 = 1 + new Random().nextInt(5);
            productinfo3.setProductNum(productNum3);
            // 商品一级目录id
            productinfo3.setFirstCategoryId(firstCategoryId);
            // 商品一级目录id
            productinfo3.setSecondCategoryId(secondCategoryId);
            // 商品一级目录id
            productinfo3.setThirdCategoryId(thirdCategoryId);
            // 商品品牌id
            int brandId3 = 3;
            productinfo3.setBrandId(brandId3);
            // 商品url
            productinfo3.setPictureUrl(pic);
            // 商品属性信息(按JSON格式存储，如--颜色:黑色,尺寸:XXS)
            String propetyInfo3 = "";
            productinfo3.setPropetyInfo(propetyInfo3);
            // 商品价格
            BigDecimal productPrice3 = price;
            productinfo3.setProductPrice(productPrice3);
            // 商品预付定金价格
            BigDecimal earnestPrice3 = price.divide(new BigDecimal(3), 2, BigDecimal.ROUND_HALF_UP); // 小数点后保留2位并四舍五入
            productinfo3.setEarnestPrice(earnestPrice3);
            // 价格类型
            int priceType3 = PriceTypeEnum.NewPersonPrice.getValue();
            productinfo3.setPriceType(priceType3);
            // 买家或卖家承担关税(0-卖家承担 1-买家承担)
            int tariffType3 = 0;
            productinfo3.setTariffType(tariffType3);
            // 物流类型
            int logisticsType3 = LogisticsTypeEnum.XloboBondedDelivery.getValue();
            productinfo3.setLogisticsType(logisticsType3);
            // 保税区
            int bondedArea3 = BondedAreaEnum.NingBo.getValue();
            productinfo3.setBondedArea(bondedArea3);
            // 商品备案号
            productinfo3.setProductRecordNo(productRecordNo);
            // SKU
            productinfo3.setSKU(sku);
            // 套餐编号
            productinfo3.setPackageNo(packageNo);
            // 商品退货渠道
            productinfo3.setProductRefundChannel(productRefundChannel);
            // 商品信息(存储格式为：Json格式)
            productinfo3.setProductDetailInfo(productDetailInfo);
            // 活动商品信息
            productinfo3.setActivityInfo(activityInfo);

            orderProductInfoList.add(productinfo);
            orderProductInfoList.add(productinfo2);
            orderProductInfoList.add(productinfo3);
            // 订单商品信息
            order.setOrderProductInfoList(orderProductInfoList);
            // 订单信息
            orderInfoList.add(order);
            placeorderBean.setUserId(userId);
            placeorderBean.setOrderInfoList(orderInfoList);
            placeorderCall.setData(false, placeorderBean);
            placeorderCall.callService();
            int orderId = placeorderCall.getOrderIds().getInt(0);
            int tradingId = placeorderCall.getTradingId();
            map.put("orderId", orderId);
            map.put("tradingId", tradingId);
        } catch (Exception e) {
            Logger.fail(e);
        }
        return map;
    }

}
